package com.xy.servlet;

import com.xy.service.HelloService;

import javax.servlet.*;
import javax.servlet.annotation.HandlesTypes;
import java.util.EnumSet;
import java.util.Set;

/**
 * @author 谢阳
 * @version 1.8.0_131
 * @date 2022/4/10 19:28
 * @description
 */
//容器启动时会将指定类型实现类子接口传递进来
@HandlesTypes(value = {HelloService.class})
public class MyServletContainerInitializer implements ServletContainerInitializer {

    /**
     * 应用启动运行
     *
     * @param set            传递的所有后代类型
     * @param servletContext 代表web应用
     * @throws ServletException
     */
    @Override
    public void onStartup(Set<Class<?>> set, ServletContext servletContext) throws ServletException {
        System.out.println("=====传入的类型=====");
        for (Class<?> clazz : set) {
            System.out.println("clazz = " + clazz);
        }

        // 注册主键
        ServletRegistration.Dynamic userServlet = servletContext.addServlet("userServlet", new UserServlet());

        userServlet.addMapping("/user");

        // 注册listener
        servletContext.addListener(UserListener.class);

        // 注册filter
        FilterRegistration.Dynamic userFilter = servletContext.addFilter("userFilter", UserFilter.class);
        userFilter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*");

    }
}
